local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseComponent());
local ConnectClass=_G.CommonOHOH.load.Connect();
local ItemTool=_G.CommonOHOH.load.ItemTool()
function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);

	self.DataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())

	self.UserDataEvent=ConnectClass.new()

end;

function M.dtor(self)
	M.super.dtor(self);
end;



function M.AddAnyThing(self,item_id,count,isSave,isSynch,...)

	local key,id=ItemTool.GetKeyAndID(item_id)

	if key=="egg" then
		-- local eg=ServerOHOH.config.Egg[tonumber(id)]
		local result= self.parent:GetComponent(_G.CommonOHOH.load.Common_PetCtrl()):HatchEgg(id,count,{})
		local other={...}
		if #other>0 then
			self.parent:SynchDataToClient(unpack(other))
		end
		return result;
	else
		return self:AddItem({[item_id]=count},isSave,isSynch,unpack({...}))
	end
end

 
--添加经验
function M.base_exp(self,count)
	print("收到增加经验--->",count)
	self.parent:GetComponent(_G.CommonOHOH.load.Common_CarInfoCtrl()):AddExp(count)
end

--添加蛋
function M.egg(self,id,count)
	local eg=ServerOHOH.config.Egg[tonumber(id)]
	self.parent:GetComponent(_G.CommonOHOH.load.Common_PetCtrl()):RubloxHatchEgg(eg,count,{})
	-- return "egg"
end
--添加遗物
function M.recipe(self,id)
	print("收到增加经验--->",id)
	self.parent:GetComponent(_G.CommonOHOH.load.Common_WorkShopCtrl()):RecipeUnLock(tonumber(id))
	-- local eg=ServerOHOH.config.Egg[tonumber(id)]
	-- self.parent:GetComponent(_G.CommonOHOH.load.Common_PetCtrl()):RubloxHatchEgg(eg,count,{})
	-- -- return "egg"
end

function M.AddItem(self,data,isSave,isSynch,...)-- 默认保存，同步
	if isSave==nil then
		isSave=true
	end

	if isSynch==nil then
		isSynch=true
	end

	local ChangeKeyList={}
	for k,v in pairs(data) do
		local f1=k
		local f2,id=ItemTool.GetKeyAndID(f1)
		if M[f1] then
			local changeKey=M[f1](self,v)
			if changeKey then
				table.insert(ChangeKeyList,changeKey)
			end
			
		elseif M[f2] then
			local changeKey=M[f2](self,id,v)
			if changeKey then
				table.insert(ChangeKeyList,changeKey)
			end
		else
			local changeKey=ItemTool.SaveData(k,self.DataBase,v)
			table.insert(ChangeKeyList,changeKey)
		end

	end

	if isSave then
		self.DataBase:Execut(function()
		end);
	end
	
	local otherKey={...}
	for _,v in pairs(otherKey) do
		table.insert(ChangeKeyList,v)
	end

	if isSynch then
		self.parent:SynchDataToClient(unpack(ChangeKeyList))
	end

	return ChangeKeyList
end;

function M.AddThingsCarePack(self,item_id,count,IsPlay,IsMustAdd,...)
	if(IsPlay==nil)then
        IsPlay=true;
    end
    if(IsMustAdd==nil)then
        IsMustAdd=false;
    end
	local key,id=ItemTool.GetKeyAndID(item_id);
	if key=="egg" then
		local eg=ServerOHOH.config.Egg[tonumber(id)]
		self.parent:GetComponent(_G.CommonOHOH.load.Common_PetCtrl()):RubloxHatchEggCareFly(eg,count,{},IsPlay,IsMustAdd)
		local other={...}
		if #other>0 then
			self.parent:SynchDataToClient(unpack(other))
		end
		elseif key=="pet" then
			if IsMustAdd then
				return self:AddItem({[item_id]=count},true,true,unpack({...}))
				else
					if(self.parent:GetComponent(_G.CommonOHOH.load.Common_PetCtrl()):isCanPutPackCount(count))then
						return self:AddItem({[item_id]=count},true,true,unpack({...}))
					end
			end
	else
		return self:AddItem({[item_id]=count},true,true,unpack({...}))
	end
end
return M